<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\api\model\User;

class Users extends Controller
{

    /**
     * 存储个人信息
     *
     * @return \think\Response
     */
    public function login(Request $request)
    {

        $APPID = config('appID');
        $AppSecret = config('appSecret');
        $wx_request_url = "https://api.weixin.qq.com/sns/jscode2session";
        //return $APPID;
        $code = $request->param("code");
        $param = array(
            'appid' => $APPID,
            'secret' => $AppSecret,
            'js_code' => $code,
            'grant_type' => 'authorization_code'
        );

        // 一个使用curl实现的get方法请求
        $arr = http_send($wx_request_url, $param, 'post');
        $arr = json_decode($arr, true);
        //return $code;
        if (isset($arr['errcode']) && !empty($arr['errcode'])) {
            return json(['code' => '1', 'message' => $arr['errmsg'], "result" => null]);
        }
        $openid = $arr['openid'];
        $session_key = $arr['session_key'];

        // 数据签名校验
        $signature = input("signature");
        $signature2 = sha1($_GET['rawData'] . $session_key);  //别用框架自带的input,会过滤掉必要的数据
        if ($signature != $signature2) {
            $msg = "shibai 1";
            return json(['code' => '数据不正确', 'message' => '获取失败', "result" => $msg]);
        }
//        //开发者如需要获取敏感数据，需要对接口返回的加密数据( encryptedData )进行对称解密
        $encryptedData = $_GET['encryptedData'];
        $iv = $_GET['iv'];
        $data = $_GET['userInfo'];
        //return $data;
        //return $iv;
        //return "$encryptedData";
////        die();
        include_once('../extend/wxdev/wxBizDataCrypt.php');
        $pc = new \WXBizDataCrypt($APPID, $session_key);
        $errCode = $pc->decryptData($encryptedData, $iv, $data);  //其中$data包含用户的所有数据
//        if ($errCode != 0) {
//            return json(['code'=>'解密错误','message'=>'获取失败',"result"=>null]);
//        }
        //return json($data);
//
        //return 1;
//        /****** */
        //生成第三方3rd_session
        $session3rd = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < 16; $i++) {
            $session3rd .= $strPol[rand(0, $max)];
        }
//        //写自己的逻辑： 操作数据库等操作

        //$dataa = json_decode($data,true);
        //var_dump($dataa['nickName']);
        //获取数据
        $user = new User();
        $res = $user ->where("openid",$openid)->select();
        //return json($res);
        if(!empty($res)){
            return json(['openid'=>$openid]);
        }
        $userdata['openid'] = $openid;
        $userdata['session3rd'] = $session3rd;
        $userdata['session_key'] = $session_key;
        $userdata['name'] = json_decode($data, true)['nickName'];
        $userdata['img_url'] = json_decode($data, true)['avatarUrl'];
        //添加到数据库

        //返回1键id
        $user_id = $user->insertGetId($userdata);
        return json(['code' => '1', 'openid' => $openid, "session" => $session3rd]);


//        //return json($data);
//        /****** */
//        //生成第三方3rd_session
//        $session3rd  = null;
//        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
//        $max = strlen($strPol)-1;
//        for($i=0;$i<16;$i++){
//            $session3rd .=$strPol[rand(0,$max)];
//        }
//        return json(['code'=>'1','message'=>'获取成功',"result"=>$session3rd]);

    }
}
